//
//  biomerTool.m
//  dongfengTest
//
//  Created by wxt on 2021/3/19.
//

#import "BiomerTool.h"
#import <LocalAuthentication/LocalAuthentication.h>
@interface BiomerTool ()
//@property (retain,nonatomic) LAContext *faceIDContext;



@end
static BiomerTool *selff = nil;
@implementation BiomerTool





+(NSInteger)isSupportBiometrics{
    NSInteger teaget;
    
    LAContext *faceIDContext = [[LAContext alloc] init];
    /*
     这里有两种验证方式可选：
     . LAPolicyDeviceOwnerAuthenticationWithBiometrics iOS8.0以上支持，只有指纹验证功能
     . LAPolicyDeviceOwnerAuthentication iOS 9.0以上支持，包含指纹验证与输入密码的验证方式
     */
    if (@available(ios 8.0,*)) {
     
//        self.faceIDContext = [[LAContext alloc] init];
        NSError *authError = nil;
        BOOL isCanEvaluatePolicy = [faceIDContext canEvaluatePolicy:(LAPolicyDeviceOwnerAuthenticationWithBiometrics) error:&authError];
        if (authError) {
            teaget = 0;
            NSLog(@"faceID error is:%@",authError.description);
            
       
            
//            [self showAlertMessage:authError.localizedDescription];
        }else{
            if (isCanEvaluatePolicy) {
                // 判断设备支持TouchID还是FaceID
                // iOS11之后LAContext新增biometryType属性，调用时候可以根据这个属性来判断当前设备是使用FaceID还是TouchID，并据此做UI样式上的调整
                if (@available(iOS 11.0, *)) {
                    switch (faceIDContext.biometryType) {
                        case LABiometryNone:
                        {
                            teaget = 0;
//                            self.BiometryTypeNumber = 0;
//                            self.BiometryTypeStr = @"";
                        }
                            break;
                        case LABiometryTypeTouchID:
                        {
                            teaget = 1;
//                            self.BiometryTypeNumber = 1;
//                            self.BiometryTypeStr = @"指纹";
                        }
                            break;
                        case LABiometryTypeFaceID:
                        {
                            teaget = 2;
//                            self.BiometryTypeStr = @"面部";
//                            self.BiometryTypeNumber = 2;
                        }
                            break;
                        default:
                            break;
                    }
                } else {
                    // Fallback on earlier versions
                    NSLog(@"iOS 11之前不需要判断 biometryType");
                    // 因为iPhoneX起始系统版本都已经是iOS11.0，所以iOS11.0系统版本下不需要再去判断是否支持faceID，直接走支持TouchID逻辑即可。
                    teaget = 1;
//                    self.BiometryTypeStr = @"指纹";
//                    self.BiometryTypeNumber = 1;
                }
                
                
                
            }else{
                teaget = 0;
            }
        }
    }else{
        
        teaget = 0;
    }
        return teaget;
}

//- (LAContext *)faceIDContext{
//    if (!_faceIDContext) {
//        _faceIDContext = [[LAContext alloc] init];
//    }
//    return _faceIDContext;
//}

@end
